package com.auction.service.impl;

import com.auction.dto.SellerApplicationDTO;
import com.auction.dto.SellerApplicationPageQueryDTO;
import com.auction.exception.BusinessException;
import com.auction.mapper.SellerApplicationMapper;
import com.auction.mapper.SellerInfoMapper;
import com.auction.mapper.UserMapper;
import com.auction.pojo.PageResult;
import com.auction.pojo.SellerApplication;
import com.auction.pojo.SellerInfo;
import com.auction.pojo.User;
import com.auction.service.SellerService;
import com.auction.vo.SellerApplicationVO;
import com.auction.vo.SellerInfoVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerApplicationMapper sellerApplicationMapper;

    @Autowired
    private SellerInfoMapper sellerInfoMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 审核卖家申请
     *
     * @param id
     * @param status
     */
    @Override
    @Transactional
    public void approve(Long id, Integer status) {
        // 1. 查询申请是否存在
        SellerApplication application = sellerApplicationMapper.selectById(id);
        if (application == null) {
            throw new BusinessException("申请不存在");
        }

        // 2. 查询申请状态
        if (application.getStatus() != 0) {
            throw new BusinessException("该申请已处理");
        }

        // 3. 更新申请状态
        application.setStatus(status);
        sellerApplicationMapper.update(application);

        // 4. 如果审核通过，创建卖家信息
        if (status == 1) {
            SellerInfo sellerInfo = new SellerInfo();
            sellerInfo.setUserId(application.getUserId());
            sellerInfo.setTotalSales(0);
            sellerInfo.setApprovalDate(LocalDateTime.now());
            sellerInfoMapper.insert(sellerInfo);
        }
    }

    /**
     * 分页查询卖家申请列表
     *
     * @param pageQueryDTO 页码和查询条件
     * @return 申请列表分页结果
     */
    @Override
    public PageResult pageQuerySellerApplications(SellerApplicationPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<SellerApplicationVO> page = sellerApplicationMapper.pageQuery(pageQueryDTO);

        Long total = page.getTotal();
        List<SellerApplicationVO> records = page.getResult();

        return new PageResult<>(total,records);
    }


    /**
     * 分页查询卖家列表
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SellerApplicationPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<SellerInfoVO> page = sellerInfoMapper.page(pageQueryDTO);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 提交卖家申请
     * 
     * @param sellerApplicationDTO
     */
    @Override
    public void apply(SellerApplicationDTO sellerApplicationDTO) {
        // 1. 查询用户是否存在
        User user = userMapper.selectById(sellerApplicationDTO.getUserId());
        if (user == null) {
            throw new BusinessException("用户不存在");
        }

        // 2. 查询是否已经是卖家
        SellerInfo sellerInfo = sellerInfoMapper.selectByUserId(sellerApplicationDTO.getUserId());
        if (sellerInfo != null) {
            throw new BusinessException("您已经是卖家，无需重复申请");
        }

        // 3. 查询是否已经提交过申请
        SellerApplication existApplication = sellerApplicationMapper.selectByUserId(sellerApplicationDTO.getUserId());
        if (existApplication != null) {
            if (existApplication.getStatus() == 0) {
                throw new BusinessException("您已提交申请，请等待审核");
            }
            if (existApplication.getStatus() == 1) {
                throw new BusinessException("您的申请已通过，无需重复申请");
            }
            // 如果是被拒绝的申请，允许重新提交
        }

        // 4. 设置申请信息
        SellerApplication application = new SellerApplication();
        BeanUtils.copyProperties(sellerApplicationDTO, application);

        // 5. 设置申请状态为审核中
        application.setStatus(0);

        // 6. 保存申请
        sellerApplicationMapper.insert(application);
    }

    /**
     * 根据用户ID查询申请状态
     * 
     * @param userId
     * @return
     */
    @Override
    public SellerApplication getByUserId(Long userId) {
        return sellerApplicationMapper.selectByUserId(userId);
    }

    /**
     * 根据用户ID查询卖家信息
     * 
     * @param userId
     * @return
     */
    @Override
    public SellerInfo getSellerInfoByUserId(Long userId) {
        return sellerInfoMapper.selectByUserId(userId);
    }
}